"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SkeletonAnimation = void 0;
var three_1 = require("three");
var SkeletonAnimation = /** @class */ (function () {
    function SkeletonAnimation() {
        this.animates = {};
        this.curr = "";
        this.clock = new three_1.Clock();
    }
    /**
     * 初始化动画 计算所需数据
     */
    SkeletonAnimation.prototype.init = function (root, animations, 
    /**
     * 默认播放第一个动画
     * 输入动画名则播放这一动画
     */
    playAnimate) {
        var mixer = new three_1.AnimationMixer(root);
        this.mixer = mixer;
        animations.reduce(function (prev, curr) {
            var action = mixer.clipAction(curr);
            prev[curr.name] = { action: action, timeScale: 1 };
            return prev;
        }, this.animates);
        //没指定 播放第一个
        if (!playAnimate) {
            var animate = Object.values(this.animates)[0];
            if (animate)
                this.play(animate.action);
        }
        else {
            var animate = this.animates[playAnimate];
            if (!animate)
                throw new Error("动画名 未找到");
            this.play(animate.action);
        }
        if (this.isNeedConsole) {
            this.console(this.isNeedConsole);
        }
        console.log(this.animates);
        this.animateFinished();
        this.animateLoop();
    };
    /**
     * 动画播放完毕回调 仅action.setLoop(LoopOnce)的动作会触发
     */
    SkeletonAnimation.prototype.animateFinished = function () {
        var _this = this;
        if (this.mixer) {
            this.mixer.addEventListener("finished", function (e) {
                _this._listenAnimateFinished &&
                    _this._listenAnimateFinished(e);
            });
        }
    };
    /**
     * 动画播放一遍回调一次
     */
    SkeletonAnimation.prototype.animateLoop = function () {
        var _this = this;
        if (this.mixer) {
            this.mixer.addEventListener("loop", function (e) {
                _this._animateLoop && _this._animateLoop(e);
            });
        }
    };
    /**
     * 动画播放完毕 触发回调 只有不循环播放的动画会触发
     */
    SkeletonAnimation.prototype.listenAnimateFinished = function (call) {
        this._listenAnimateFinished = call;
    };
    /**
     * 动画播放完一次 触发一次回调
     */
    SkeletonAnimation.prototype.listenAnimateLoop = function (call) {
        this._animateLoop = call;
    };
    SkeletonAnimation.prototype.update = function () {
        if (!this.mixer)
            return;
        var delta = this.clock.getDelta();
        this.mixer.update(delta);
    };
    /**
     * 提供dui调试
     */
    SkeletonAnimation.prototype.console = function (gui) {
        var _this = this;
        if (!gui)
            return;
        if (!this.mixer)
            this.isNeedConsole = gui;
        var _loop_1 = function (name_1) {
            var fn = function () {
                _this.animates[name_1].action.play();
                _this.prepareCrossFade(_this.animates[_this.curr].action, _this.animates[name_1].action, 0.3);
                _this.curr = name_1;
            };
            gui.add({ fn: fn }, "fn").name(name_1);
        };
        for (var _i = 0, _a = Object.keys(this.animates); _i < _a.length; _i++) {
            var name_1 = _a[_i];
            _loop_1(name_1);
        }
    };
    /**
     * 播放动画 而不是切换动作 切换过渡动画使用prepareCrossFade
     */
    SkeletonAnimation.prototype.play = function (action) {
        this.setWeight(action, 1);
        action.time = 0;
        action.fadeIn(0.3);
        action.play();
        this.curr = action.getClip().name;
    };
    /**
     * 切换动画
     */
    SkeletonAnimation.prototype.toggle = function (targetName, duration, timeScale) {
        if (duration === void 0) { duration = 0.2; }
        if (!this.animates[targetName])
            throw new Error("\u6CA1\u6709 ".concat(targetName, " \u8FD9\u4E2A\u52A8\u4F5C"));
        if (targetName === this.curr)
            return;
        this.animates[targetName].action.play();
        if (timeScale) {
            this.animates[targetName].timeScale = timeScale;
        }
        this.prepareCrossFade(this.animates[this.curr].action, this.animates[targetName].action, duration);
        this.curr = targetName;
    };
    /**
     * 准备过渡
     */
    SkeletonAnimation.prototype.prepareCrossFade = function (startAction, endAction, duration) {
        // if (this.curr === "idle") {
        this.executeCrossFade(startAction, endAction, duration);
        // } else {
        // this.synchronizeCrossFade(startAction, endAction, duration);
        // }
    };
    /**
     * 等待当前动作播放完 再过渡下一个动作
     */
    SkeletonAnimation.prototype.synchronizeCrossFade = function (startAction, endAction, duration) {
        var _this = this;
        if (!this.mixer)
            return;
        var onLoopFinished = function (event) {
            if (event.action === startAction) {
                _this.mixer.removeEventListener("loop", onLoopFinished);
                _this.executeCrossFade(startAction, endAction, duration);
            }
        };
        this.mixer.addEventListener("loop", onLoopFinished);
    };
    /**
     * 执行过渡
     */
    SkeletonAnimation.prototype.executeCrossFade = function (startAction, endAction, duration) {
        this.setWeight(endAction, 1);
        endAction.time = 0;
        startAction.crossFadeTo(endAction, duration, false);
    };
    /**
     * 设置动作权重
     */
    SkeletonAnimation.prototype.setWeight = function (action, weight) {
        action.enabled = true;
        // 时间缩放 决定播放速度 小于1 变慢
        var timeScale = this.animates[action.getClip().name].timeScale;
        // action.setEffectiveTimeScale(1);
        action.setEffectiveWeight(weight);
        // 整个时间缩放 决定播放速度 小于1 变慢
        this.mixer.timeScale = timeScale;
    };
    return SkeletonAnimation;
}());
exports.SkeletonAnimation = SkeletonAnimation;
